package cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.OnboardSupplyPlanPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.OnboardSupplyPlanRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.OnboardSupplyPlanSaveReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupplyplan.vo.OnboardSupplyPlanSimpleRespVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.onboardsupplyplan.OnboardSupplyPlanDO;
import cn.iocoder.yudao.module.acm.service.onboardsupplyplan.OnboardSupplyPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 航机管理-机供品生产计划")
@RestController
@RequestMapping("/pplan/onboard-supply-plan")
@Validated
public class OnboardSupplyPlanController {

    @Resource
    private OnboardSupplyPlanService onboardSupplyPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建航机管理-机供品生产计划")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:create')")
    public CommonResult<Long> createOnboardSupplyPlan(@Valid @RequestBody OnboardSupplyPlanSaveReqVO createReqVO) {
        return success(onboardSupplyPlanService.createOnboardSupplyPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新航机管理-机供品生产计划")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:update')")
    public CommonResult<Boolean> updateOnboardSupplyPlan(@Valid @RequestBody OnboardSupplyPlanSaveReqVO updateReqVO) {
        onboardSupplyPlanService.updateOnboardSupplyPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除航机管理-机供品生产计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:delete')")
    public CommonResult<Boolean> deleteOnboardSupplyPlan(@RequestParam("id") Long id) {
        onboardSupplyPlanService.deleteOnboardSupplyPlan(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除航机管理-机供品生产计划")
                @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:delete')")
    public CommonResult<Boolean> deleteOnboardSupplyPlanList(@RequestParam("ids") List<Long> ids) {
        onboardSupplyPlanService.deleteOnboardSupplyPlanListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得航机管理-机供品生产计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:query')")
    public CommonResult<OnboardSupplyPlanRespVO> getOnboardSupplyPlan(@RequestParam("id") Long id) {
        OnboardSupplyPlanRespVO respVO = onboardSupplyPlanService.getOnboardSupplyPlan(id);
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得航机管理-机供品生产计划分页")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:query')")
    public CommonResult<PageResult<OnboardSupplyPlanRespVO>> getOnboardSupplyPlanPage(@Valid OnboardSupplyPlanPageReqVO pageReqVO) {
        PageResult<OnboardSupplyPlanDO> pageResult = onboardSupplyPlanService.getOnboardSupplyPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OnboardSupplyPlanRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得航机管理-机供品生产计划分页")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:query')")
    public CommonResult<PageResult<OnboardSupplyPlanSimpleRespVO>> getOnboardSupplyPlanSimplePage(@Valid OnboardSupplyPlanPageReqVO pageReqVO) {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        PageResult<OnboardSupplyPlanDO> pageResult = onboardSupplyPlanService.getOnboardSupplyPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OnboardSupplyPlanSimpleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出航机管理-机供品生产计划 Excel")
    @PreAuthorize("@ss.hasPermission('acm:onboard-supply-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOnboardSupplyPlanExcel(@Valid OnboardSupplyPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OnboardSupplyPlanDO> list = onboardSupplyPlanService.getOnboardSupplyPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "航机管理-机供品生产计划.xls", "数据", OnboardSupplyPlanRespVO.class,
                        BeanUtils.toBean(list, OnboardSupplyPlanRespVO.class));
    }

}